package com.tenton.controller;

import com.tenton.MyException.DataNotExist;
import com.tenton.MyException.InsufficientQuantity;
import com.tenton.pojo.ExCommodity;
import com.tenton.pojo.ImCommodity;
import com.tenton.service.CommodityService;
import com.tenton.service.ExCommodityService;
import com.tenton.service.ImCommodityService;
import com.tenton.service.StockService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Date: 2021/1/9
 * @Author: Tenton
 * @Description:
 */
@ApiIgnore
@Controller
@ApiOperation("管理员商品退货管理")
@RequestMapping("/admin/flatform/exCommodity")
public class AExCommodityController {
    @Autowired
    ExCommodityService exCommodityService;
    @Autowired
    ImCommodityService imCommodityService;
    @Autowired
    StockService stockService;
    @Autowired
    CommodityService commodityService;

    /**
     * 获取所有退货信息
     *
     * @param model
     * @param pn
     * @return
     */
    @ApiOperation("获取所有退货信息")
    @GetMapping("/getAll")
    public String getAll(ModelMap model,
                         @ApiParam("页码") @RequestParam(defaultValue = "1", required = true, value = "pn") Integer pn
    ) {
        exCommodityService.getAll(pn, model);
        return "admin/commodity/getExCommodityList";

    }

    /**
     * 根据id查询退货信息
     *
     * @param id
     * @param model
     * @return
     */
    @ApiOperation("根据id查询退货信息")
    @PostMapping("/getExCommodity")
    public String getExCommodity(int id, Model model) {
        exCommodityService.selectByPrimaryKey(id, model);
        return "admin/commodity/getExCommodity";
    }

    /**
     * 编辑退货信息
     *
     * @param exCommodity
     * @param model
     * @return
     */
    @ApiOperation("编辑退货信息")
    @GetMapping("/edit")
    public String edit(ExCommodity exCommodity, Model model) {
        exCommodityService.selectByPrimaryKey(exCommodity.getId(), model);
        return "admin/commodity/editExCommodity";
    }

    /**
     * 修改退货信息
     *
     * @param exCommodity
     * @param model
     * @param session
     * @param request
     * @return
     */
    @ApiOperation("修改退货信息")
    @PostMapping("/update")
    public String update(ExCommodity exCommodity, Model model, HttpSession session, HttpServletRequest request) {
        Boolean flag = null;
        try {
            flag = exCommodityService.update(exCommodity, model,session);
            if (flag) {
                return "redirect:/admin/flatform/exCommodity/getAll";
            }
        } catch (InsufficientQuantity insufficientQuantity) {
            request.setAttribute("msg", "商品库存不足，请重新输入！");
            return "admin/commodity/editExCommodity";
        }
        return null;
    }

    /**
     * 删除退货信息
     *
     * @param id
     * @param session
     * @return
     */
    @ApiOperation("删除退货信息")
    @GetMapping("/delete")
    public String delete(int id,HttpSession session) {
        exCommodityService.deleteByPrimaryKey(id,session);
        return "redirect:/admin/flatform/exCommodity/getAll";
    }

    /**
     * 跳转到增加页面
     *
     * @return
     */
    @ApiOperation("跳转到增加页面")
    @GetMapping("/toAdd")
    public String toAdd() {
        return "admin/commodity/addExCommodity";

    }

    /**
     * 先判断数据库有没有，有就更新，没有就新增
     *
     * @param exCommodity
     * @param session
     * @return
     */
    @ApiOperation("新增退货信息")
    @PostMapping("/insert")
    public String insert(ExCommodity exCommodity, HttpSession session, HttpServletRequest request) {
        try {
            exCommodityService.insert(exCommodity,session);
            return "redirect:/admin/flatform/exCommodity/getAll";
        } catch (DataNotExist dataNotExist) {
            request.setAttribute("msg", "退货信息为空！");
            return "admin/commodity/addExCommodity";
        } catch (NumberFormatException numberFormatException) {
            request.setAttribute("msg", "商品库存不足！");
            return "admin/commodity/addExCommodity";
        }
    }

    /**
     * 时间格式化
     *
     * @param binder
     */
    @InitBinder
    protected void init(ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }

    /**
     * 商品名称模糊查询
     *
     * @param model
     * @param comName
     * @param pn
     * @return
     */
    @ApiOperation("商品名称模糊查询")
    @GetMapping("/getByParams")
    public String getByParams(ModelMap model,
                              @ApiParam("商品名称") @RequestParam(value = "comName", required = false) String comName,
                              @ApiParam("页码") @RequestParam(defaultValue = "1", required = true, value = "pn") Integer pn
    ) {
        exCommodityService.getByParams(comName, pn, model);
        return "admin/commodity/getExCommodityByParams";

    }

    /**
     * 根据id查询进货信息
     *
     * @param imId
     * @return
     */
    @ApiOperation("根据id查询进货信息")
    @PostMapping("/getExCommodities")
    public ImCommodity imCommodity(int imId) {
        return imCommodityService.selectById(imId);
    }

    /**
     * 查询商品信息
     *
     * @param comId
     * @param model
     * @return
     */
    @ApiOperation("通过comId查询商品信息")
    @GetMapping("/getCommodityByComId")
    public String getCommodityByComId(int comId, Model model, HttpServletRequest request) {
        try {
            commodityService.getCommodityByComId(comId, model);
        } catch (DataNotExist dataNotExist) {
            request.setAttribute("msg", "商品信息不存在！");
        }
        return "admin/commodity/addExCommodity";
    }
}

